package com.ssm.Dao;

import com.ssm.Utils.DBCPUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {

    //注册判断
    public boolean checkUser(String name) {
        String sql = "select name from user where name=?";
        boolean bl=true;
        Object[] objects= {name};
        ResultSet resultSet= DBCPUtils.executeQuery(sql,objects);
        try {
             while (resultSet.next()){
                 bl=false;
             }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBCPUtils.closeAll(DBCPUtils.rs, DBCPUtils.pstmt, DBCPUtils.connection);
        }
        return bl;
    }

    //登录判断
    public boolean checkUser(String name, String password) {
        String sql="select name,password from user where name=? and password=?";
        boolean bl=false;
        Object[] objects={name,password};
        ResultSet resultSet = DBCPUtils.executeQuery(sql,objects);
        try {
            while (resultSet.next()){
                bl=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //关闭数据库的方法
            DBCPUtils.closeAll(resultSet, DBCPUtils.pstmt, DBCPUtils.connection);
        }
//        System.out.println("bl:"+bl);
        return bl;
    }

    //用户信息存数据库
    public Boolean register(String name, String password, String email) {
        String sql="insert into user(name,password,email) values(?,?,?)";
        Object[] objects={name,password,email};

        //存数据库的方法
        boolean bool= DBCPUtils.executeUpdate(sql,objects);
        return bool;
    }

    //通过name查找id
    public int findByName(String name) {

        String sql="select id from user where name=?";

        Object[] objects={name};

        ResultSet resultSet = DBCPUtils.executeQuery(sql, objects);
        int userId = 0;
        try {
            while (resultSet.next()){
               userId=resultSet.getInt("id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //关闭数据库的方法
            DBCPUtils.closeAll(resultSet, DBCPUtils.pstmt, DBCPUtils.connection);
        }

        return userId;

    }
}